/*
 * This file is part of animus, a GUI for the command line app "GHost++"
 *
 * http://code.google.com/p/animus/
 *
 * Copyright (C) 2008-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;
using System.Xml.Serialization;
using System.ComponentModel;
using FlagsEnumTypeConverter;

namespace animus
{
    [Serializable]
    /*[
     XmlInclude(typeof(ConsoleTriggerFontChange)),
     XmlInclude(typeof(ConsoleTriggerTrayBalloon)),
     XmlInclude(typeof(ConsoleTriggerPlaySound))
    ]*/
    public abstract class ConsoleTrigger : ICloneable
    {
        public Object Clone()
        {
            return this.MemberwiseClone();
        }
        public ConsoleTrigger()
        {
            m_Name = this.GetDescription();
        }
        public ConsoleTrigger(string triggerText) : this()
        {
            m_TriggerText = triggerText;
        }
        protected string m_TriggerText = "^.*$";
        [Category("Basic")]
        [Description("The regular expression all textlines are matched against to check if this trigger should execute.")]
        public string TriggerText
        {
            get { return m_TriggerText; }
            set { m_TriggerText = value; }
        }
        protected string m_Name;
        [Category("Basic")]
        [Description(@"A title given by you to clarify what this trigger does. For example: ""someone whispered me"".")]
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }
        protected RegexOptions m_RegexOptions = RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase;
        [Category("Advanced")]
        [Description("Options used for regex matching")]
        [TypeConverter(typeof(FlagsEnumConverter))]
        public System.Text.RegularExpressions.RegexOptions RegexOptions
        {
            get { return m_RegexOptions; }
            set { m_RegexOptions = value; }
        }
        protected bool m_SuppressOutput = false;

        [Browsable(false)]
        public bool SuppressOutput
        {
          get { return m_SuppressOutput; }
        }

        public void Evaluate(string line)
        {
            m_SuppressOutput = false;
            Match match = Regex.Match(line, m_TriggerText, m_RegexOptions);
            if (match.Success)
                Triggered(line, match);

        }
        protected abstract void Triggered(string line, Match match);
        public abstract string GetDescription();
        public virtual string GetParameters()
        {
            return "Trigger: '" + m_TriggerText +"'";
        }
        public override string ToString()
        {
            return GetDescription();
        }
    }

    
}
